<?php defined('SYSPATH') OR die('No direct access allowed.');

class Anaks_Controller extends Template_Controller {
    protected $restrict_guest = TRUE;

    public function __construct() {
        if (Router::$method == 'view' || Router::$method == 'view_marks') {
            $this->restrict_outside_roles = array('administrator','motivator');
        }

        parent::__construct();
    }

    public function index () {
        
    }

    public function view ($motivator_id) {
        $motivator = ORM::factory ('user', $motivator_id);
        $group = ORM::factory('group')->where('motivator_id', $motivator->id)->find();
        $anaks = ORM ::factory('user')->where('group_id', $group->id)->orderby('nim_fakultas', 'ASC')->find_all();
        $anaks_nogroup = Database::instance()->query (
            "SELECT *
            FROM `users` JOIN `roles_users` ON `id` = `user_id`
            WHERE `role_id` = 1 AND `group_id` = 0"
        );
        $tasks = ORM::factory('task')->find_all();
        $tasks_users = Database::instance()->select()->from('tasks_users')->get();

        $this->title .= 'Group '.$group->name;

        $this->content->motivator = $motivator;
        $this->content->group = $group;
        $this->content->anaks = $anaks;
        $this->head->anaks = $anaks_nogroup;
        $this->content->tasks = $tasks;
        $this->content->tasks_users = $tasks_users;

        //buat nampilin events_users
        $events = ORM::factory('event')->find_all();
        $this->content->events = $events;

        //proses form add anak
        if ( ($_POST) && ($_POST['anak_name']!= "")){
            $new_anak_name = substr($_POST['anak_name'],0,strlen($_POST['anak_name'])-2);
            $new_anak = ORM::factory('user')->where('name',$new_anak_name)->find();
            if ($new_anak->id != 0) {
                //update ke tabel user
                $query = Database::instance()->query (
                    "UPDATE  `sparta`.`users`
                    SET  `group_id` =  '".$group->id."'
                    WHERE  `users`.`id` = ".$new_anak->id."
                    LIMIT 1 ;"
                );
            }
            url::redirect('anaks/view/'.$motivator->id);
        }
    }

    public function view_marks ($motivator_id) {        
        $motivator = ORM::factory ('user', $motivator_id);
        $this->title .= 'Anak Motivator of '.$motivator->name;

        $group = ORM::factory('group')->where('motivator_id', $motivator->id)->find();
        $anaks = ORM ::factory('user')->where('group_id', $group->id)->find_all();
        $all_tasks = ORM::factory('task')->find_all();
        //$tasks_users = Database::instance()->select()->from('tasks_users')->get();

        $this->content->motivator = $motivator;
        $this->content->group = $group;
        $this->content->anaks = $anaks;
        $this->content->all_tasks = $all_tasks;
        //$this->content->tasks_users = $tasks_users;

        //proses tasks yang akan ditampilkan
        if(isset($_POST['checkgroup'])) {
            $tasks = array();
            foreach($_POST['checkgroup'] as $key => $val) {
                $tasks[] = ORM::factory('task', $val);
            }
            $this->content->tasks = $tasks;
        }
        else {
            $this->content->tasks = $all_tasks;
        }

        $this->content->task_id = array();
        foreach($this->content->tasks as $task)
            $this->content->task_id[] = $task->id;

        if($_POST){
            foreach($this->content->anaks as $anak){
                foreach($this->content->tasks as $task){
                    $query = Database::instance()->query("SELECT * FROM tasks_users WHERE user_id = '$anak->id' AND task_id = '$task->id'");
                    
                    if(isset($_POST[$anak->id.'_'.$task->id])){
                        $mark = $_POST[$anak->id.'_'.$task->id];
                        if($mark != ""){
                            foreach($query as $task_user)
                                $time = $task_user->submit_time;

                            if($query->count() == 1) {//ada
                                Database::instance()->query("UPDATE tasks_users SET mark = '$mark', submit_time = '$time' WHERE user_id = '$anak->id' AND task_id = '$task->id'");
                            } else{
                                Database::instance()->query("INSERT INTO tasks_users(user_id, task_id, submit_time, mark) VALUES ('$anak->id', '$task->id','', '$mark')");
                            }
                        }
                    }
                }
            }
        }
    }

    
}
